﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Infrastructure.Data;
using System.Data.Entity.Validation;
using System.Diagnostics;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfCheckInQuery : ICheckInQuery
    {
        public IEnumerable<CheckIn> GetAll()
        {
            var context = new hmsDBEntities();
            return context.checkIns.Select(ci => new CheckIn()
            {
                CheckInID = ci.checkinID,
                GuestID = ci.guestID,
                UserID = ci.userID,
                NumOfAdults = ci.numOfAdults,
                NumOfChildren = Convert.ToInt32(ci.numOfChildren),
                CheckInDateTime = ci.checkinDateTime,
                DesiredCheckOutDateTime = ci.desiredCheckOutDateTime,
                RoomType = ci.roomType,
                BedType = ci.bedType,
                SmokingOrNot = ci.smokingOrNot,
                AddRemarks = ci.addRemarks,
                LateCheckOutRequest = ci.lateCheckOutReq,
                RoomNum = ci.roomNum,
                PaymentMode = ci.paymentMode

            });

        }
        public void AddCheckIn(CheckIn ci)
        {

                var context = new hmsDBEntities();
                var newCheckIn = context.checkIns.Create();

                newCheckIn.checkinID = ci.CheckInID;
                newCheckIn.guestID = ci.GuestID;
                newCheckIn.userID = ci.UserID;
                newCheckIn.numOfAdults = ci.NumOfAdults;
                newCheckIn.numOfChildren = ci.NumOfChildren;
                newCheckIn.checkinDateTime = ci.CheckInDateTime;
                newCheckIn.desiredCheckOutDateTime = ci.DesiredCheckOutDateTime;
                newCheckIn.roomType = ci.RoomType;
                newCheckIn.bedType = ci.BedType;
                newCheckIn.smokingOrNot = ci.SmokingOrNot;
                newCheckIn.addRemarks = ci.AddRemarks;
                newCheckIn.lateCheckOutReq = ci.LateCheckOutRequest;
                newCheckIn.roomNum = ci.RoomNum;
                newCheckIn.paymentMode = ci.PaymentMode;

                context.checkIns.Add(newCheckIn);
                context.SaveChanges();
 
        }

        public void UpdateCheckIn(CheckIn ci)
        {
            var context = new hmsDBEntities();
            var checkinToUpdate = context.checkIns.FirstOrDefault(u => u.checkinID == ci.CheckInID);
            checkinToUpdate.numOfAdults = ci.NumOfAdults;
            checkinToUpdate.numOfChildren = ci.NumOfChildren;
            checkinToUpdate.checkinDateTime = ci.CheckInDateTime;
            checkinToUpdate.desiredCheckOutDateTime = ci.DesiredCheckOutDateTime;
            checkinToUpdate.roomType = ci.RoomType;
            checkinToUpdate.bedType = ci.BedType;
            checkinToUpdate.smokingOrNot = ci.SmokingOrNot;
            checkinToUpdate.addRemarks = ci.AddRemarks;
            checkinToUpdate.lateCheckOutReq = ci.LateCheckOutRequest;
            checkinToUpdate.roomNum = ci.RoomNum;
            checkinToUpdate.paymentMode = ci.PaymentMode;
            context.SaveChanges();



        }
        public void DeleteCheckIn(string checkinID)
        {
            var context = new hmsDBEntities();
            var checkinToDelete = context.checkIns.FirstOrDefault(d => d.checkinID == checkinID);
            context.checkIns.Remove(checkinToDelete);
        }

        public bool CheckInRecordAdded(string checkinID)
        {
            var context = new hmsDBEntities();
            var SuccessOrFail = context.checkIns.FirstOrDefault(d => d.checkinID == checkinID);
            if (SuccessOrFail != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string GetCheckinID(string guestID)
        {
            var context = new hmsDBEntities();
            var checkinIDToRetrieve = context.checkIns.FirstOrDefault(d => d.guestID == guestID);
            string checkinID = checkinIDToRetrieve.checkinID;
            return checkinID;
        }

        public DateTime GetCheckinDateTime(string checkinID)
        {
            var context = new hmsDBEntities();
            var checkinDateTimeToRetrieve = context.checkIns.FirstOrDefault(d => d.checkinID == checkinID);
            DateTime checkinDateTime = checkinDateTimeToRetrieve.checkinDateTime;
            return checkinDateTime;
        }

        public string GetPaymentMode(string checkinID)
        {
            var context = new hmsDBEntities();
            var paymentModeToRetrieve = context.checkIns.FirstOrDefault(d => d.checkinID == checkinID);
            string pM = paymentModeToRetrieve.paymentMode;
            return pM;
        }
    }
}
